package com.algorithms.contests.srm481rn1;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class GreedyGift
{

	int[][] give=new int[10][10];
	Map<String,Integer> output= new LinkedHashMap<String,Integer>();
	private int calculateForPerson(int slNo)
	{  
		int fAmt=0;
		if(give!=null)
		{
			for(int i=0;i<give.length;i++ )
			{
				if(i==slNo)
				{
					fAmt=give[i][i] +fAmt;
				}
				else
				{
					fAmt= -give[slNo][i]+give[i][slNo]+fAmt;
				}
			}
		}
		return fAmt;
		
	}
	
	private void parse(String[] array)
	{
		int totalPersons;
		
		totalPersons=Integer.parseInt(array[0]);
		for(int i=1;i<=totalPersons;i++)
		{
			output.put(array[i], i-1);
		}
		
		for(int i=totalPersons+1;i<array.length;)
		{
			int giveArrayloc=output.get(array[i]);
			String amountNum=array[i+1];
			int amount=Integer.parseInt(amountNum.split(" ")[0]);
			int totGive=Integer.parseInt(amountNum.split(" ")[1]);
			int keepWithYourself=0 ;
			int giveEach=0;
			if(totGive!=0)
			{
				 keepWithYourself =amount%totGive;
				 giveEach = amount/totGive;
				
				give[giveArrayloc][giveArrayloc]=keepWithYourself;
				
				for(int j=0;j<totGive;j++)
				{
					int givePersonloc=output.get(array[i+2+j]);
					give[giveArrayloc][givePersonloc]=giveEach;
				}
			}
			else
			{
				System.out.println("Nobody to give");
			}
			i=i+2+totGive;
		}
	}
	
	public String[] calculateFinal()
	{
		String[] outPutStringArray=new String[output.size()];
		int count=0;
		for(Entry<String, Integer> nameVal: output.entrySet())
		{
			Integer calculated=calculateForPerson(nameVal.getValue());
			output.put(nameVal.getKey(),calculated);
			outPutStringArray[count++]=nameVal.getKey()+  " " +calculated;
		}
		return outPutStringArray;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		String[]input= new String[]{
				"5",
				"dave",
				"laura",
				"owen",
				"vick",
				"amr",
				"dave",
				"200 3",
				"laura",
				"owen",
				"vick",
				"owen",
				"500 1",
				"dave",
				"amr",
				"150 2",
				"vick",
				"owen",
				"laura",
				"0 2",
				"amr",
				"vick",
				"vick",
				"0 0"
				};
		
		GreedyGift greedGift= new GreedyGift();
		greedGift.parse(input);
		greedGift.calculateFinal();

	}

}
